﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;

public partial class SanPham : System.Web.UI.Page
{
    private bool inputOK = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Multiview1.ActiveViewIndex = 0;
        //DisplayList();
        //if (!IsPostBack)
        //{
        //    string sql = "select TenHang from hangxe";
        //    DataTable dt = DataBase.GetDataTable(sql);
        //    foreach (DataRow row in dt.Rows)
        //    {
        //        cmbHang.Items.Add(row["TenHang"].ToString());
        //    }
        //}
        if (!IsPostBack)
        {
            if (Session["AdminName"] != null)
            {
                Multiview1.ActiveViewIndex = 0;
                DisplayList();
                string sql = "select TenHang from hangxe";
                DataTable dt = DataBase.GetDataTable(sql);
                foreach (DataRow row in dt.Rows)
                {
                    cmbHang.Items.Add(row["TenHang"].ToString());
                }
            }
            else
                Response.Redirect("Default.aspx");
        }
    }

    protected void DisplayList()
    {
        string sql = string.Format(@" 
                                    SELECT 
                                        a.MaXe, 
                                        a.TenXe,
                                        b.TenHang,
                                        a.DoiXe
                                    FROM
                                        ThongTinCoBan a INNER JOIN hangxe b
                                        ON a.MaHangXe=b.MaHangXe
                                    ");
        DataTable dt = DataBase.GetDataTable(sql);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void DisplayListEdit(string maxe)
    {
        string sql = string.Format(@"
                                    SELECT *
                                    FROM ThongTinCoBan a
                                            INNER JOIN hangxe b ON a.MaHangXe=b.MaHangXe
                                            INNER JOIN model c ON a.ModelID=c.ModelID
                                            INNER JOIN NhienLieu nl ON a.MaXe=nl.MaXe
                                            INNER JOIN ChiTiet ct ON a.MaXe=ct.MaXe
                                            INNER JOIN ChongTrom ctr ON a.MaXe=ctr.MaXe
                                            INNER JOIN DongCo dc ON a.MaXe=dc.MaXe
                                            INNER JOIN gia g ON a.MaXe=g.MaXe
                                            INNER JOIN HopSoChuyenDong hs ON a.MaXe=hs.MaXe
                                            INNER JOIN KichThuocTrongLuong kt ON a.MaXe=kt.MaXe
                                            INNER JOIN PhanhDieuKhien pdk ON a.MaXe=pdk.MaXe
                                            INNER JOIN PhanhLop pl ON a.MaXe=pl.MaXe
                                            INNER JOIN TuiKhiAnToan tkat ON a.MaXe=tkat.MaXe
                                            INNER JOIN ThongSoKiThuat tskt ON a.MaXe=tskt.MaXe
                                            INNER JOIN thongsokhac tsk ON a.MaXe=tsk.MaXe
                                    WHERE a.MaXe='{0}'
                                    ", maxe);
        DataTable dt = DataBase.GetDataTable(sql);
        foreach (DataRow dtr in dt.Rows)
        {
            //thong tin co ban
            txtMaXe.Text = dtr["MaXe"].ToString();
            txtTenXe.Text = dtr["TenXe"].ToString();
            cmbHang.Text = dtr["TenHang"].ToString();
            GetModel(cmbHang.Text);
            cmbModel.Text = dtr["Model"].ToString();
            txtPhanHangModel.Text = dtr["DoiXe"].ToString();
            cmbNamSX.Text = dtr["NamSX"].ToString();
            cmbXuatXu.Text = dtr["XuatXu"].ToString();
            cmbTinhTrang.Text = dtr["TinhTrang"].ToString();
            txtSoKM.Text = dtr["SoKM"].ToString();
            txtNoiThat.Text = dtr["NoiThat"].ToString();
            txtNgoaiThat.Text = dtr["NgoaiThat"].ToString();
            txtSoCua.Text = dtr["SoCua"].ToString();
            txtSoCho.Text = dtr["SoCho"].ToString();
            TextBox1.Text = dtr["Image"].ToString();
            imgXe.ImageUrl = @"~\Pictures\SmallImage\" + dtr["Image"].ToString() + ".jpg";

            //nhien lieu
            txtHeThongNapNhienLieu.Text = dtr["HeThongNap"].ToString();
            txtNhienLieu.Text = dtr["NhienLieu"].ToString();
            
            //Hop so chuyen dong
            txtHopSo.Text = dtr["HopSo"].ToString();
            txtDanDong.Text = dtr["DanDong"].ToString();
            txtMucTieuThu.Text = dtr["MucTieuThuNhienLieu"].ToString();

            //Tui khi an toan
            SetChekBox(dtr["DanhChoNguoiLai"].ToString(), cbTK_LaiXe);
            SetChekBox(dtr["PhiaTruoc"].ToString(), cbTK_PhiaTruoc);
            SetChekBox(dtr["PhiaSau"].ToString(), cbTK_PhiaSau);
            SetChekBox(dtr["HaiBen"].ToString(), cbTK_HaiBen);
            SetChekBox(dtr["PhiaTren"].ToString(), cbTK_Treo);

            //phanh dieu khien
            SetChekBox(dtr["ChongBoCung"].ToString(), cb_ABS);
            SetChekBox(dtr["PhanBoLuc"].ToString(), cb_EBD);
            SetChekBox(dtr["TuDongCanBang"].ToString(), cb_ESP);
            SetChekBox(dtr["DieuKhien"].ToString(), cb_DieuKhien);
            SetChekBox(dtr["HoTro"].ToString(), cb_Lui);
            SetChekBox(dtr["KiemSoatTruot"].ToString(), cb_Truot);
            SetChekBox(dtr["TroLuc"].ToString(), cb_EBA);

            //khoa chong trom
            SetChekBox(dtr["ChotCua"].ToString(),cb_ChotCua );
            SetChekBox(dtr["KhoaTuDong"].ToString(), cb_KhoaTuDong);
            SetChekBox(dtr["KhoaTuXa"].ToString(), cb_KhoaTuXa);
            SetChekBox(dtr["KhoaDongCo"].ToString(), cb_KhoaDongCo);
            SetChekBox(dtr["BaoTrom"].ToString(), cb_ChongTrom);

            //thong so khac
             SetChekBox(dtr["DenSuongMu"].ToString(), cb_DSuongMu);
             SetChekBox(dtr["DenCanhBaoDayAnToan"].ToString(), cb_DayAnToan);
             SetChekBox(dtr["DenPhanhPhu"].ToString(), cb_DPhanhPhu);

            //thong so ki thuat
             SetChekBox(dtr["ThietBiDinhVi"].ToString(), cbTBDvi);
            SetChekBox(dtr["CuaSoNoc"].ToString(), cbCuaSoNoc);
            SetChekBox(dtr["KinhChinhDien"].ToString(), cbKinhChinhDien);
            SetChekBox(dtr["TayTroLuc"].ToString(), cbTaylaiTroLuc);
            SetChekBox(dtr["DieuHoaTruoc"].ToString(), cbDieuHoaTruoc);
            SetChekBox(dtr["DieuHoaSau"].ToString(), cbDieuHoaSau);
            SetChekBox(dtr["HoTroTuDong"].ToString(), cbHoTroXeTuDong);
            SetChekBox(dtr["XayKinhSau"].ToString(), cbXayKinhSau);
            SetChekBox(dtr["QuatKinhPhiaSau"].ToString(), cbQuatKinhPhiaSau);
            SetChekBox(dtr["KinhMau"].ToString(), cbKinhMau);
            SetChekBox(dtr["ManHinhLCD"].ToString(), cbManLCD);
            txtThietBiGiaiTri.Text=dtr["ThietBiGiaiTri"].ToString();
            txtThietBiKhac.Text=dtr["ThietBiKhac"].ToString();
            txtGhe.Text=dtr["Ghe"].ToString();

            //  kich thuoc
            txtDaiRongCao.Text = dtr["DaiRongCao"].ToString();
            txtChieuDaiCoSo.Text = dtr["ChieuDaiCoSo"].ToString();
            txtChieuRongCoSo.Text = dtr["ChieuRongCoSo"].ToString();
            txtTrongLuong.Text = dtr["TrongLuong"].ToString();
            txtDungTichBinhNL.Text = dtr["DungTichBinhNhienLieu"].ToString();

            //Dong Co
            txtDongCo.Text = dtr["DongCo"].ToString();
            txtKieuDongCo.Text = dtr["KieuDongCo"].ToString();
            txtDungTichXiLanh.Text = dtr["DungTichXiLanh"].ToString();

            //Phanh lop
            txtPhanh.Text = dtr["Phanh"].ToString();
            txtGiamXoc.Text = dtr["GiamXoc"].ToString();
            txtLopXe.Text = dtr["LopXe"].ToString();
            txtVanhMamXe.Text = dtr["VanhMamXe"].ToString();

            //chi tiet
            txtChiTietNoiThat.Text = dtr["CTNoiThat"].ToString();
            txtChiTietNgoaiThat.Text = dtr["CTNgoaiThat"].ToString();
            txtChiTietTBiAN.Text = dtr["CTThietBiAnNinh"].ToString();

            //gia ban
            txtGiaBan.Text = dtr["GiaBan"].ToString();
        }                        
    }

    private void SetChekBox(string values,CheckBox ck)
    {
        if (Int32.Parse(values) == 1)
        {
            ck.Checked = true;
        }
        if (Int32.Parse(values) == 0)
        {
            ck.Checked = false;
        }
    }

    private void GetModel(string hangxe)
    {
        string command = string.Format(@"
                                SELECT Model
                                FROM model AS a
                                INNER JOIN hangxe AS b ON a.MaHangXe=b.MaHangXe
                                WHERE b.TenHang=N'{0}'
                                ", hangxe);

        cmbModel.Items.Clear();
        cmbModel.Items.Add("");
        DataTable dtb1 = DataBase.GetDataTable(command);
        foreach (DataRow dtr in dtb1.Rows)
        {
            cmbModel.Items.Add(dtr[0].ToString());
        }
    }

    protected void cmbHang_SelectedIndexChanged(object sender, EventArgs e)
    {
        string hangxe = cmbHang.SelectedItem.Text;
        GetModel(hangxe);
        Multiview1.ActiveViewIndex = 1;
    }

    protected void cmbHang_TextChanged(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string maxe= GridView1.Rows[e.RowIndex].Cells[2].Text;
        string sql = "delete from ThongTinCoBan where MaXe='" + maxe + "'";
        string sql1 = string.Format(@"DELETE FROM ChiTiet
                                            WHERE MaXe='{0}'
                                        ", maxe);
        string sql2 = "DELETE FROM ChongTrom WHERE MaXe='" + maxe + "'";
        string sql3 = "DELETE FROM DongCo WHERE MaXe='" + maxe + "'";
        string sql4 = "DELETE FROM gia WHERE MaXe='" + maxe + "'";
        string sql5 = "DELETE FROM HopSoChuyenDong WHERE MaXe='" + maxe + "'";
        string sql6 = "DELETE FROM KichThuocTrongLuong WHERE MaXe='" + maxe + "'";
        string sql7 = "DELETE FROM NhienLieu WHERE MaXe='" + maxe + "'";
        string sql8 = "DELETE FROM PhanhDieuKhien WHERE MaXe='" + maxe + "'";
        string sql9 = "DELETE FROM PhanhLop WHERE MaXe='" + maxe + "'";
        string sql10 = "DELETE FROM TuiKhiAnToan WHERE MaXe='" + maxe + "'";
        string sql11= "DELETE FROM ThongSoKiThuat WHERE MaXe='" + maxe + "'";
        string sql12 = "DELETE FROM thongsokhac WHERE MaXe='" + maxe + "'";

       
        DataBase.ExecuteNonQuery(sql1);
        DataBase.ExecuteNonQuery(sql2);
        DataBase.ExecuteNonQuery(sql3);
        DataBase.ExecuteNonQuery(sql4);
        DataBase.ExecuteNonQuery(sql5);
        DataBase.ExecuteNonQuery(sql6);
        DataBase.ExecuteNonQuery(sql7);
        DataBase.ExecuteNonQuery(sql8);
        DataBase.ExecuteNonQuery(sql9);
        DataBase.ExecuteNonQuery(sql10);
        DataBase.ExecuteNonQuery(sql11);
        DataBase.ExecuteNonQuery(sql12); 
        DataBase.ExecuteNonQuery(sql);
        DisplayList();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;
        DisplayListEdit(GridView1.Rows[index].Cells[2].Text);
        Multiview1.ActiveViewIndex = 1;
        btnUpdate.Text = "Update";
        lblmaxe.Visible = true;
        txtMaXe.Visible = true;
        lblThongBao.Visible = false;
    }

    protected void btnThem_Click(object sender, EventArgs e)
    {
        Multiview1.ActiveViewIndex = 1;
        lblmaxe.Visible = false;
        txtMaXe.Visible = false;
        lblThongBao.Visible = false;
        TextBox1.Text = "";
        imgXe.ImageUrl = "";
        txtChieuDaiCoSo.Text = "";
        txtChieuRongCoSo.Text = "";
        txtChiTietNoiThat.Text = "";
        txtChiTietNgoaiThat.Text = "";
        txtChiTietTBiAN.Text = "";
        txtDaiRongCao.Text = "";
        txtDanDong.Text = "";
        txtDongCo.Text = "";
        txtDungTichBinhNL.Text = "10";
        txtDungTichXiLanh.Text = "1000";
        txtGhe.Text = "";
        txtGiaBan.Text = "";
        txtGiamXoc.Text = "";
        txtHeThongNapNhienLieu.Text = "";
        txtHopSo.Text = "";
        txtKieuDongCo.Text = "";
        txtLopXe.Text = "";
        txtMaXe.Text = "";
        txtMucTieuThu.Text = "100";
        txtNoiThat.Text = "";
        txtNgoaiThat.Text = "";
        txtNhienLieu.Text = "";
        txtPhanHangModel.Text = "";
        txtPhanh.Text = "";
        txtSoCua.Text = "4";
        txtSoCho.Text = "4";
        txtSoKM.Text = "0";
        txtTenXe.Text = "";
        txtThietBiGiaiTri.Text = "";
        txtThietBiKhac.Text = "";
        txtThongSoKTKhac.Text = "";
        txtTrongLuong.Text = "100000";
        txtVanhMamXe.Text = "";
        cmbHang.Text = "";
        cmbModel.Text = "";
        btnUpdate.Text = "Add";
    }

    private bool checkFileType(string FileName)
    {
        string ext = Path.GetExtension(FileName);
        if (ext.Equals(".jpg")||ext.Equals(".JPG"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            if (checkFileType(FileUpload1.FileName))
            {
                FileUpload1.SaveAs(Server.MapPath(@"~\Pictures\SmallImage\") + FileUpload1.FileName);
            }
            imgXe.ImageUrl = @"~\Pictures\SmallImage\" + FileUpload1.FileName;
            TextBox1.Text = FileUpload1.FileName; 
            string s = FileUpload1.FileName;
            string[] words = s.Split('.');
            string imgName = words[0];
            TextBox1.Text = imgName;
            Multiview1.ActiveViewIndex = 1;
        }
    }

    protected string GetMaHang(string tenHang)
    {
        string mahang = "";
        string sql = "select MaHangXe from hangxe where TenHang=N'" + tenHang + "'";
        DataTable dt = DataBase.GetDataTable(sql);
        foreach (DataRow row in dt.Rows)
        {
            mahang = row["MaHangXe"].ToString();
        }
        return mahang;
    }

    protected string GetModelID(string model)
    {
        string modelid = "";
        string sql = "select ModelID from model where Model=N'" + model + "'";
        DataTable dt = DataBase.GetDataTable(sql);
        foreach (DataRow row in dt.Rows)
        {
            modelid = row["ModelID"].ToString();
        }
        return modelid;
    }

    protected int TestCheckBox(CheckBox cb)
    {
        if (cb.Checked == true)
            return 1;
        return 0;
    }

    protected void checkInput()
    {
        lblError1.Visible = checkRegex(txtGiaBan.Text) ? false : true;
        lblError2.Visible = checkRegex(txtSoKM.Text) ? false : true;
        lblError3.Visible = checkRegex(txtSoCua.Text) ? false : true;
        lblError4.Visible = checkRegex(txtSoCho.Text) ? false : true;
        lblError5.Visible = checkRegex(txtMucTieuThu.Text) ? false : true;
        lblError6.Visible = checkRegex(txtTrongLuong.Text) ? false : true;
        lblError7.Visible = checkRegex(txtDungTichBinhNL.Text) ? false : true;
        lblError8.Visible = checkRegex(txtDungTichXiLanh.Text) ? false : true;
        if (checkRegex(txtGiaBan.Text) && checkRegex(txtSoKM.Text) && checkRegex(txtSoCua.Text)
            && checkRegex(txtSoCho.Text) && checkRegex(txtMucTieuThu.Text) && checkRegex(txtTrongLuong.Text)
            && checkRegex(txtDungTichBinhNL.Text) && checkRegex(txtDungTichXiLanh.Text))
            inputOK = true;
        else
            inputOK = false;
    }

    protected bool checkRegex(string input)
    {
        Regex myRegex = new Regex(@"^[0-9]*\.?[0-9]*$");

        if (!myRegex.IsMatch(input))
        {           
            return false;
        }
        else
        {            
            return true;
        }                
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        checkInput();
        if (inputOK)
        {
            if (btnUpdate.Text.Equals("Add"))
            {
                if (cmbHang.Text != "")
                {
                    Insert();
                    DisplayList();
                    Multiview1.ActiveViewIndex = 0;
                }
                else
                {
                    lblThongBao.Visible = true;
                    Multiview1.ActiveViewIndex = 1;
                }
            }
            else if (btnUpdate.Text.Equals("Update"))
            {
                Update();
                Multiview1.ActiveViewIndex = 0;
            }
        }
        else
            Multiview1.ActiveViewIndex = 1;
        
    }

    private string SinhMa(string mahangxe,string mamodel)
    {
        double soTT;
        string maxecuoi;
        string maso;

        string sqlmaxe = string.Format(@" SELECT MaXe
                                          FROM ThongTinCoBan
                                          WHERE MaHangXe=N'{0}' AND ModelID=N'{1}'
                                        ", mahangxe,mamodel);
        DataTable dtb = new DataTable();

        dtb = DataBase.GetDataTable(sqlmaxe);       
        if (dtb.Rows.Count <= 0)
        {
            soTT = 0;
        }
        else 
        {
            maxecuoi = dtb.Rows[(dtb.Rows.Count - 1)][0].ToString();
            maso = maxecuoi.Substring((maxecuoi.Length - 6), 6);            
            soTT = double.Parse(maso) + 1;
        }
        string model = mamodel==""?"":mamodel.Substring((mahangxe.Length), 2);
        return (mahangxe + model.ToUpper() + soTT.ToString("000000"));
    }

    private void Insert()
    {
        string maHang = GetMaHang(cmbHang.SelectedItem.Text);
        string modelid = GetModelID(cmbModel.SelectedItem.Text);

        string maXe = SinhMa(maHang,modelid);
        
        // insert bang Thong Tin Co Ban
        int sokm = txtSoKM.Text==""?0:int.Parse(txtSoKM.Text);
        int socua = txtSoCua.Text==""?0:int.Parse(txtSoCua.Text);
        int socho = txtSoCho.Text==""?0:int.Parse(txtSoCho.Text);       
        string sql = string.Format(@"
                                    INSERT INTO ThongTinCoBan
                                    (
                                        MaXe,
                                        TenXe,
                                        MaHangXe,
                                        DoiXe,
                                        ModelID,
                                        NamSX,
                                        XuatXu,
                                        TinhTrang,
                                        SoKM,
                                        NoiThat,
                                        NgoaiThat,
                                        SoCua,
                                        SoCho,
                                        NgayNhap,
                                        DaBan,
                                        Image
                                     )
                                     Values
                                     (
                                        N'{0}',
                                        N'{1}',
                                        N'{2}',
                                        N'{3}',
                                        N'{4}',
                                        {5},
                                        N'{6}',
                                        N'{7}',
                                        {8},
                                        N'{9}',
                                        N'{10}',
                                        {11},
                                        {12},
                                        '{13}',
                                        0,
                                        '{14}'
                                      )
                                    ", maXe,
                                       txtTenXe.Text,
                                       maHang,
                                       txtPhanHangModel.Text,
                                       modelid,
                                       cmbNamSX.Text,
                                       cmbXuatXu.Text,
                                       cmbTinhTrang.Text,
                                       sokm,
                                       txtNoiThat.Text,
                                       txtNgoaiThat.Text,
                                       socua,
                                       socho,
                                       DateTime.Now,
                                       TextBox1.Text);
        DataBase.ExecuteNonQuery(sql);

        //Insert bang TuiKhiAnToan
        string sqlTuiKhi = string.Format(@"
                                            INSERT INTO tuikhiantoan
                                            (
                                                MaXe,
                                                DanhChoNguoiLai,
                                                PhiaTruoc,
                                                PhiaSau,
                                                HaiBen,
                                                PhiaTren)
                                            Values
                                            (
                                                N'{0}',
                                                 {1},
                                                 {2},
                                                 {3},
                                                 {4},
                                                 {5}
                                            )
                                            ", maXe,
                                             TestCheckBox(cbTK_LaiXe),
                                             TestCheckBox(cbTK_PhiaTruoc),
                                             TestCheckBox(cbTK_PhiaSau),
                                             TestCheckBox(cbTK_HaiBen),
                                             TestCheckBox(cbTK_Treo)
                                             );
        DataBase.ExecuteNonQuery(sqlTuiKhi);

        //Nhien Lieu
        string sqlNL = string.Format(@"
                                        INSERT INTO nhienlieu
                                        (
                                            MaXe,
                                            HeThongNap,
                                            NhienLieu
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            N'{1}',
                                            N'{2}'
                                        )
                                        ", maXe,
                                         txtHeThongNapNhienLieu.Text,
                                         txtNhienLieu.Text
                                         );
        DataBase.ExecuteNonQuery(sqlNL);

        //Hop So
        decimal muctieuthu = txtMucTieuThu.Text == "" ? 0 : Decimal.Parse(txtMucTieuThu.Text);
        string sqlHS = string.Format(@"
                                        INSERT INTO hopsochuyendong
                                        (
                                            MaXe,
                                            HopSo,
                                            DanDong,
                                            MucTieuThuNhienLieu
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            N'{1}',
                                            N'{2}',
                                            N'{3}'
                                        )
                                        ", maXe,
                                         txtHopSo.Text,
                                         txtDanDong.Text,
                                         muctieuthu);
        DataBase.ExecuteNonQuery(sqlHS);

        //Khoa chong trom
        string sqlCT = string.Format(@"
                                        INSERT INTO ChongTrom
                                        (
                                            MaXe,
                                            ChotCua,
                                            KhoaTuDong,
                                            KhoaTuXa,
                                            KhoaDongCo,
                                            BaoTrom
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            {1},
                                            {2},
                                            {3},
                                            {4},
                                            {5}
                                        )
                                        ", maXe,
                                         TestCheckBox(cb_ChotCua),
                                         TestCheckBox(cb_KhoaTuDong),
                                         TestCheckBox(cb_KhoaTuXa),
                                         TestCheckBox(cb_KhoaDongCo),
                                         TestCheckBox(cb_ChongTrom));
        DataBase.ExecuteNonQuery(sqlCT);

        // Thong so khac
        string sqlK = string.Format(@"
                                        INSERT INTO thongsokhac
                                        (
                                            MaXe,
                                            DenSuongMu,
                                            DenCanhBaoDayAnToan,
                                            DenPhanhPhu
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            {1},
                                            {2},
                                            {3}
                                        )
                                        ", maXe,
                                         TestCheckBox(cb_DSuongMu),
                                         TestCheckBox(cb_DayAnToan),
                                         TestCheckBox(cb_DPhanhPhu));
        DataBase.ExecuteNonQuery(sqlK);

        // thong so kt
        string sqlKT = string.Format(@"
                                        INSERT INTO thongsokithuat
                                        (
                                            MaXe,
                                            ThietBiDinhVi,
                                            CuaSoNoc,
                                            KinhChinhDien,
                                            TayTroLuc,
                                            Ghe,
                                            DieuHoaTruoc,
                                            DieuHoaSau,
                                            HoTroTuDong,
                                            XayKinhSau,
                                            QuatKinhPhiaSau,
                                            KinhMau,
                                            ManHinhLCD,
                                            ThietBiGiaiTri,
                                            ThietBiKhac
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            {1},
                                            {2},
                                            {3},
                                            {4},
                                            N'{5}',
                                            {6},
                                            {7},
                                            {8},
                                            {9},
                                            {10},
                                            {11},
                                            {12},
                                            N'{13}',
                                            N'{14}'
                                        )
                                        ", maXe,
                                         TestCheckBox(cbTBDvi),
                                         TestCheckBox(cbCuaSoNoc),
                                         TestCheckBox(cbKinhChinhDien),
                                         TestCheckBox(cbTaylaiTroLuc),
                                         txtGhe.Text,
                                         TestCheckBox(cbDieuHoaTruoc),
                                         TestCheckBox(cbDieuHoaSau),
                                         TestCheckBox(cbHoTroXeTuDong),
                                         TestCheckBox(cbXayKinhSau),
                                         TestCheckBox(cbQuatKinhPhiaSau),
                                         TestCheckBox(cbKinhMau),
                                         TestCheckBox(cbManLCD),
                                         txtThietBiGiaiTri.Text,
                                         txtThietBiKhac.Text);
        DataBase.ExecuteNonQuery(sqlKT);

        // kich thuoc trong luong
        string sqlTL = string.Format(@"
                                        INSERT INTO kichthuoctrongluong
                                        (
                                            MaXe,
                                            DaiRongCao,
                                            ChieuDaiCoSo,
                                            ChieuRongCoSo,
                                            TrongLuong,
                                            DungTichBinhNhienLieu
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            N'{1}',
                                            N'{2}',
                                            N'{3}',
                                            N'{4}',
                                            N'{5}'
                                        )
                                        ", maXe,
                                         txtDaiRongCao.Text,
                                         txtChieuDaiCoSo.Text,
                                         txtChieuRongCoSo.Text,
                                         txtTrongLuong.Text,
                                         txtDungTichBinhNL.Text);
        DataBase.ExecuteNonQuery(sqlTL);

        // Dong co
        int dungtich = txtDungTichXiLanh.Text == "" ? 0 : Int32.Parse(txtDungTichXiLanh.Text);
        string sqlDC = string.Format(@"
                                        INSERT INTO dongco
                                        (
                                            MaXe,
                                            DongCo,
                                            KieuDongCo,
                                            DungTichXiLanh
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            N'{1}',
                                            N'{2}',
                                            {3}
                                        )
                                        ", maXe,
                                         txtDongCo.Text,
                                         txtKieuDongCo.Text,
                                         dungtich);
        DataBase.ExecuteNonQuery(sqlDC);

        //Phanh lop
        string sqlPL = string.Format(@"
                                        INSERT INTO phanhlop
                                        (
                                            MaXe,
                                            Phanh,
                                            GiamXoc,
                                            LopXe,
                                            VanhMamXe
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            N'{1}',
                                            N'{2}',
                                            N'{3}',
                                            N'{4}'
                                        )
                                        ", maXe,
                                         txtPhanh.Text,
                                         txtGiamXoc.Text,
                                         txtLopXe.Text,
                                         txtVanhMamXe.Text);
        DataBase.ExecuteNonQuery(sqlPL);

        //chi tiet
        string sqlCTiet = string.Format(@"
                                        INSERT INTO chitiet
                                        (
                                            MaXe,
                                            CTNoiThat,
                                            CTNgoaiThat,
                                            CTThietBiAnNinh
                                        )
                                        Values
                                        (
                                            N'{0}',
                                            N'{1}',
                                            N'{2}',
                                            N'{3}'
                                        )
                                        ", maXe,
                                         txtChiTietNoiThat.Text,
                                         txtChiTietNgoaiThat.Text,
                                         txtChiTietTBiAN.Text);
        DataBase.ExecuteNonQuery(sqlCTiet);

        //Phanh điều khiển
        string sqlPhanhDK = string.Format(@"
                                                INSERT INTO PhanhDieuKhien
                                                (
                                                    MaXe,
                                                    CHongBoCung,
                                                    PhanBoLuc,
                                                    TroLuc,
                                                    TuDongCanBang,
                                                    DieuKhien,
                                                    HoTro,
                                                    KiemSoatTruot 
                                                )
                                                VALUES
                                                (
                                                    N'{0}',
                                                    {1},
                                                    {2},
                                                    {3},
                                                    {4},
                                                    {5},
                                                    {6},
                                                    {7}
                                                )
                                                ", maXe,
                                             TestCheckBox(cb_ABS),
                                             TestCheckBox(cb_EBD),
                                             TestCheckBox(cb_EBA),
                                             TestCheckBox(cb_ESP),
                                             TestCheckBox(cb_DieuKhien),
                                             TestCheckBox(cb_Lui),
                                             TestCheckBox(cb_Truot));
        DataBase.ExecuteNonQuery(sqlPhanhDK);

        //Gia ban
        double gia = txtGiaBan.Text==""?0:double.Parse(txtGiaBan.Text);
        string sqlGia = string.Format(@"  
                                        INSERT INTO gia
                                        (
                                            MaXe,
                                            GiaBan
                                         )
                                         VALUES
                                        (
                                            N'{0}',
                                            {1}
                                        )
                                        ", maXe,
                                         gia);
        DataBase.ExecuteNonQuery(sqlGia);
    }

    private void Update()
    {
        string path = TextBox1.Text;
        string maHang = GetMaHang(cmbHang.SelectedItem.Text);
        string modelid = GetModelID(cmbModel.SelectedItem.Text);

        //  bang Thong Tin Co Ban
        int sokm = txtSoKM.Text == "" ? 0 : int.Parse(txtSoKM.Text);
        int socua = txtSoCua.Text == "" ? 0 : int.Parse(txtSoCua.Text);
        int socho = txtSoCho.Text == "" ? 0 : int.Parse(txtSoCho.Text);   
        string sql = string.Format(@"
                                    UPDATE ThongTinCoBan
                                    SET
                                        TenXe=N'{0}',
                                        MaHangXe=N'{1}',
                                        DoiXe=N'{2}',
                                        ModelID=N'{3}',
                                        NamSX={4},
                                        XuatXu=N'{5}',
                                        TinhTrang=N'{6}',
                                        SoKM={7},
                                        NoiThat=N'{8}',
                                        NgoaiThat=N'{9}',
                                        SoCua={10},
                                        SoCho={11},
                                        Image='{12}'
                                     WHERE
                                        MaXe=N'{13}'
                                    ",
                                       txtTenXe.Text,
                                       maHang,
                                       txtPhanHangModel.Text,
                                       modelid,
                                       cmbNamSX.Text,
                                       cmbXuatXu.Text,
                                       cmbTinhTrang.Text,
                                       sokm,
                                       txtNoiThat.Text,
                                       txtNgoaiThat.Text,
                                       socua,
                                       socho,
                                       path,
                                       txtMaXe.Text);
        DataBase.ExecuteNonQuery(sql);

        // bang TuiKhiAnToan
        string sqlTuiKhi = string.Format(@"
                                            Update TuiKhiAnToan
                                            SET
                                                DanhChoNguoiLai={0},
                                                PhiaTruoc= {1},
                                                PhiaSau= {2},
                                                HaiBen= {3},
                                                PhiaTren= {4}
                                             WHERE
                                                MaXe=N'{5}'
                                            ",
                                             TestCheckBox(cbTK_LaiXe),
                                             TestCheckBox(cbTK_PhiaTruoc),
                                             TestCheckBox(cbTK_PhiaSau),
                                             TestCheckBox(cbTK_HaiBen),
                                             TestCheckBox(cbTK_Treo),
                                             txtMaXe.Text
                                             );
        DataBase.ExecuteNonQuery(sqlTuiKhi);

        //Nhien Lieu
        string sqlNL = string.Format(@"
                                        UPDATE NhienLieu
                                        SET
                                           HeThongNap= N'{0}',
                                           NhienLieu= N'{1}'
                                        WHERE
                                            MaXe=N'{2}'
                                        ",
                                         txtHeThongNapNhienLieu.Text,
                                         txtNhienLieu.Text,
                                         txtMaXe.Text
                                         );
        DataBase.ExecuteNonQuery(sqlNL);

        //Hop So
        decimal muctieuthu = txtMucTieuThu.Text == "" ? 0 : Decimal.Parse(txtMucTieuThu.Text);
        string sqlHS = string.Format(@"
                                        UPDATE HopSoChuyenDong
                                        SET
                                            HopSo=N'{0}',
                                            DanDong=N'{1}',
                                            MucTieuThuNhienLieu={2}
                                         WHERE
                                            MaXe=N'{3}'   
                                        ",
                                         txtHopSo.Text,
                                         txtDanDong.Text,
                                         muctieuthu,
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlHS);

        //Khoa chong trom
        string sqlCT = string.Format(@"
                                        UPDATE ChongTrom
                                        SET
                                            ChotCua={0},
                                            KhoaTuDong={1},
                                            KhoaTuXa={2},
                                            KhoaDongCo={3},
                                            BaoTrom={4}
                                        WHERE
                                            MaXe=N'{5}'
                                        ",
                                         TestCheckBox(cb_ChotCua),
                                         TestCheckBox(cb_KhoaTuDong),
                                         TestCheckBox(cb_KhoaTuXa),
                                         TestCheckBox(cb_KhoaDongCo),
                                         TestCheckBox(cb_ChongTrom),
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlCT);

        // Thong so khac
        string sqlK = string.Format(@"
                                        UPDATE thongsokhac
                                        SET
                                            DenSuongMu={0},
                                            DenCanhBaoDayAnToan={1},
                                            DenPhanhPhu={2}
                                        WHERE
                                            MaXe=N'{3}'
                                        ",
                                         TestCheckBox(cb_DSuongMu),
                                         TestCheckBox(cb_DayAnToan),
                                         TestCheckBox(cb_DPhanhPhu),
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlK);

        // thong so kt
        string sqlKT = string.Format(@"
                                        UPDATE ThongSoKiThuat
                                        SET
                                            ThietBiDinhVi={0},
                                            CuaSoNoc={1},
                                            KinhChinhDien={2},
                                            TayTroLuc={3},
                                            Ghe=N'{4}',
                                            DieuHoaTruoc={5},
                                            DieuHoaSau={6},
                                            HoTroTuDong={7},
                                            XayKinhSau={8},
                                            QuatKinhPhiaSau={9},
                                            KinhMau={10},
                                            ManHinhLCD={11},
                                            ThietBiGiaiTri=N'{12}',
                                            ThietBiKhac=N'{13}'
                                         WHERe
                                            MaXe=N'{14}'
                                        ",
                                         TestCheckBox(cbTBDvi),
                                         TestCheckBox(cbCuaSoNoc),
                                         TestCheckBox(cbKinhChinhDien),
                                         TestCheckBox(cbTaylaiTroLuc),
                                         txtGhe.Text,
                                         TestCheckBox(cbDieuHoaTruoc),
                                         TestCheckBox(cbDieuHoaSau),
                                         TestCheckBox(cbHoTroXeTuDong),
                                         TestCheckBox(cbXayKinhSau),
                                         TestCheckBox(cbQuatKinhPhiaSau),
                                         TestCheckBox(cbKinhMau),
                                         TestCheckBox(cbManLCD),
                                         txtThietBiGiaiTri.Text,
                                         txtThietBiKhac.Text,
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlKT);

        // kich thuoc trong luong
        string sqlTL = string.Format(@"
                                        UPDATE KichThuocTrongLuong
                                        SET
                                            DaiRongCao=N'{0}',
                                            ChieuDaiCoSo=N'{1}',
                                            ChieuRongCoSo=N'{2}',
                                            TrongLuong=N'{3}',
                                            DungTichBinhNhienLieu=N'{4}'
                                         WHERE
                                            MaXe=N'{5}'
                                        ",
                                         txtDaiRongCao.Text,
                                         txtChieuDaiCoSo.Text,
                                         txtChieuRongCoSo.Text,
                                         txtTrongLuong.Text,
                                         txtDungTichBinhNL.Text,
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlTL);

        // Dong co
        int dungtich = txtDungTichXiLanh.Text==""?0:Int32.Parse(txtDungTichXiLanh.Text);
        string sqlDC = string.Format(@"
                                        UPDATE DongCo
                                        SET
                                            DongCo=N'{0}',
                                            KieuDongCo=N'{1}',
                                            DungTichXiLanh={2}
                                        WHERE
                                            MaXe=N'{3}'
                                        ",
                                         txtDongCo.Text,
                                         txtKieuDongCo.Text,
                                         dungtich,
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlDC);

        //Phanh lop
        string sqlPL = string.Format(@"
                                        UPDATE PhanhLop
                                        SET
                                           Phanh= N'{0}',
                                           GiamXoc= N'{1}',
                                           LopXe= N'{2}',
                                           VanhMamXe= N'{3}'
                                        WHERE
                                           MaXe= N'{4}'
                                        ",
                                         txtPhanh.Text,
                                         txtGiamXoc.Text,
                                         txtLopXe.Text,
                                         txtVanhMamXe.Text,
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlPL);

        //chi tiet
        string sqlCTiet = string.Format(@"
                                        UPDATE ChiTiet
                                        SET
                                           CTNoiThat= N'{0}',
                                           CTNgoaiThat= N'{1}',
                                           CTThietBiAnNinh= N'{2}'
                                        WHERE
                                           MaXe= N'{3}'
                                        ",
                                         txtChiTietNoiThat.Text,
                                         txtChiTietNgoaiThat.Text,
                                         txtChiTietTBiAN.Text,
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlCTiet);

        //Phanh điều khiển
        string sqlPhanhDK = string.Format(@"
                                                UPDATE PhanhDieuKhien
                                                SET
                                                   ChongBoCung= {0},
                                                   PhanBoLuc= {1},
                                                   TroLuc= {2},
                                                   TuDongCanBang= {3},
                                                   DieuKhien= {4},
                                                   HoTro= {5},
                                                   KiemSoatTruot= {6}
                                                WHERE
                                                    MaXe=N'{7}'
                                                ",
                                             TestCheckBox(cb_ABS),
                                             TestCheckBox(cb_EBD),
                                             TestCheckBox(cb_EBA),
                                             TestCheckBox(cb_ESP),
                                             TestCheckBox(cb_DieuKhien),
                                             TestCheckBox(cb_Lui),
                                             TestCheckBox(cb_Truot),
                                             txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlPhanhDK);
        
        //Gia ban
        string sqlGia = string.Format(@"  
                                        UPDATE gia
                                         SET
                                            GiaBan={0}
                                         WHERE
                                            MaXe=N'{1}'
                                        ",
                                         decimal.Parse(txtGiaBan.Text),
                                         txtMaXe.Text);
        DataBase.ExecuteNonQuery(sqlGia);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.DisplayList();
    }
}
